# Adogs
# 时间: 2021/7/25 14:04
import enum

import pygame.image
from actor.xiao_tie import XiaoTie
from game import SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_HALF_WIDTH, SCREEN_HALF_HEIGHT, FULL_HEIGHT, FULL_WIDTH


class ScenePassStatus(enum.IntEnum):
    """
    场景状态
    """
    final_win = 0
    final_fail = 1
    over = 3
    level_pass = 4
    level_fail = 5
    normal = 6
    next_sence = 7


class BaseScene:
    """
    基本场景函数
    方法:
    1.__init__(xiao_tie, background_path)
    2.edge()
    3.get_current_surface()
    4.draw_actor()
    5.run(down_flag, key_list)
    6.get_pass_status()
    """
    def __init__(self, xiao_tie: XiaoTie, background_path):
        """
        初始化函数
        :param xiao_tie: 主角人物
        :param background_path: 背景图片路径
        """
        self.xiao_tie = xiao_tie
        self.path = background_path
        self.background_image = pygame.image.load(self.path)

        # 设置窗口大小
        self.current_surface = pygame.Surface((SCREEN_WIDTH, SCREEN_HEIGHT)).convert()

        # 判断场景大小是否符合所设定的游戏窗口的大小
        if (self.background_image.get_width() <= SCREEN_WIDTH) and (
                self.background_image.get_height() <= SCREEN_HEIGHT):
            self.sub_scene_flag = False     # sub_scene_flag用于判断是否需要分割场景为当前窗口大小
        else:
            self.sub_scene_flag = True

        self.pass_status = ScenePassStatus.normal

    def edge(self):
        """
         地图场景的边界判断
        :return: 人物相对屏幕的x,y坐标
        """
        pos_x = self.xiao_tie.pos_x
        pos_y = self.xiao_tie.pos_y

        if pos_x < SCREEN_HALF_WIDTH:
            view_x = 0
        elif pos_x > FULL_WIDTH - SCREEN_HALF_WIDTH - 1:    # FULL_WIDTH是地图的宽度,后者是屏幕的半宽度
            view_x = FULL_WIDTH - SCREEN_WIDTH - 1
        else:
            view_x = pos_x - SCREEN_HALF_WIDTH

        if pos_y < SCREEN_HALF_HEIGHT:
            view_y = 0
        elif pos_y > FULL_HEIGHT - SCREEN_HALF_HEIGHT - 1:      # 同上
            view_y = FULL_HEIGHT - SCREEN_HEIGHT - 1
        else:
            view_y = pos_y - SCREEN_HALF_HEIGHT

        return view_x, view_y

    def get_current_surface(self):
        """
        分割当前窗口
        :return: 当前窗口
        """
        if not self.sub_scene_flag:
            self.current_surface.blit(self.background_image, self.current_surface.get_rect())
        else:
            self.view_x, self.view_y = self.edge()
            image = self.background_image.subsurface((self.view_x, self.view_y, SCREEN_WIDTH, SCREEN_HEIGHT))
            self.current_surface.blit(image, self.current_surface.get_rect())
        self.draw_actor()
        return self.current_surface

    def draw_actor(self):
        """
        绘制人物
        交由派生类写方法
        :return: 无返回值
        """
        pass

    def run(self, down_flag, key_list):
        """
        场景运行函数
        交由派生类写方法
        :param down_flag:
        :param key_list:
        :return:
        """
        pass

    def get_pass_status(self):
        """
        获取通关状态
        :return: 当前状态，下一个场景
        """
        return self.pass_status, None
